<?php
namespace Admin\Controller;
use Net\Requests;
use Think\Controller;
use Admin\Model\RecycleOrderModel;
use Admin\Model\MemberModel;
use Admin\Model\OptionModel;
use Admin\Model\RegionModel;

/**
* 订单类
*/
class OrderController extends Controller
{
	private $member_id = 0;
	function __construct(){
		parent::__construct();
		if($_SESSION['member_id']=='0' || $_SESSION['member_id']){
			$this->member_id = $_SESSION['member_id'];
		}else{
			returnMobileJson(0,'未登录');
		}
		$this->RecycleOrder = new RecycleOrderModel();
	}

	/**
	 * 获取回收订单统计数据
	 * @return json 回收订单统计数据
	 */
	public function recycle_order_count(){
		
		/**
		 * 获取订单数
		 * @param  array $engineer_list 二维数组
		 * @return array                订单数
		 */
		function get_order_count($arr_list){
			$RecycleOrder = new RecycleOrderModel();
			$data['total'] = 0;
			$data['waiting_handle'] = 0;
			$data['waiting_list'] = 0;
			$data['waiting_recycle'] = 0;
			$data['waiting_confirm'] = 0;
			$data['waiting_comment'] = 0;
			$data['cancel'] = 0;
			$data['completed'] = 0;
			foreach ($arr_list as $key => $arr) {
				$data['total'] += $RecycleOrder->get_order_count($arr['re_id']);
				$data['waiting_handle'] += $RecycleOrder->get_order_count($arr['re_id'],0);
				$data['waiting_list'] += $RecycleOrder->get_order_count($arr['re_id'],1);
				$data['waiting_recycle'] += $RecycleOrder->get_order_count($arr['re_id'],5);
				$data['waiting_confirm'] += $RecycleOrder->get_order_count($arr['re_id'],3);
				$data['waiting_comment'] += $RecycleOrder->get_order_count($arr['re_id'],6);
				$data['cancel'] += $RecycleOrder->get_order_count($arr['re_id'],6);
				$data['completed'] += $RecycleOrder->get_order_count($arr['re_id'],4);
			}
			return $data;
		}
		$Member = new MemberModel();
		if($this->member_id){
			$member = $Member->get_member($this->member_id);
			if($member['m_type']==1){
				$Region = new RegionModel();
				$arr_list = $Region->get_region($member['city_id']);
			}else if($member['m_type']==2){
				$arr_list = $Member->get_member_list(0,null,0,$this->member_id);
			}
		}else{
			$arr_list[0]['re_id'] = FALSE;
		}
		$data = get_order_count($arr_list);
		returnMobileJson(200,'获取成功',$data);
	}
	// public function recycle_order_count(){
	// 	/**
	// 	 * 获取订单数
	// 	 * @param  array $engineer_list 二维数组
	// 	 * @return [type]                [description]
	// 	 */
	// 	function get_order_count($engineer_list){
	// 		$RecycleOrder = new RecycleOrderModel();
	// 		$data['total'] = 0;
	// 		$data['waiting_handle'] = 0;
	// 		$data['waiting_list'] = 0;
	// 		$data['waiting_recycle'] = 0;
	// 		$data['waiting_confirm'] = 0;
	// 		$data['completed'] = 0;
	// 		foreach ($engineer_list as $key => $engineer) {
	// 			$data['total'] += $RecycleOrder->get_order_count($engineer['member_id']);
	// 			$data['waiting_handle'] += $RecycleOrder->get_order_count($engineer['member_id'],0);
	// 			$data['waiting_list'] += $RecycleOrder->get_order_count($engineer['member_id'],1);
	// 			$data['waiting_recycle'] += $RecycleOrder->get_order_count($engineer['member_id'],5);
	// 			$data['waiting_confirm'] += $RecycleOrder->get_order_count($engineer['member_id'],3);
	// 			$data['completed'] += $RecycleOrder->get_order_count($engineer['member_id'],7);
	// 		}
	// 		return $data;
	// 	}
	// 	$Member = new MemberModel();
	// 	if(!$this->menber_id){
	// 		$m_type = $Member->get_member($this->member_id)['m_type'];
	// 	}else{
	// 		$m_type = 0;
	// 	}
	// 	if($m_type==1){
	// 		// 城市合伙人统计数据
	// 		$have_engineer_list = $Member->get_member_list(0,null,3,$this->member_id);
	// 		$didi_engineer_list = $Member->get_member_list(0,null,4,$this->member_id);
	// 		$union_business_list = $Member->get_member_list(0,null,2,$this->member_id);
	// 		$union_engineer_list = array();
	// 		foreach ($union_business_list as $key => $union_business) {
	// 			$union_engineer_arr = $Member->get_member_list(0,null,5,$union_business['member_id']);
	// 			$union_engineer_list = array_merge($union_engineer_list,$union_engineer_arr);
	// 		}
	// 		$engineer_list = array_merge($have_engineer_list,$didi_engineer_list,$union_engineer_list);
	// 	}else if($m_type==2){
	// 		// 联盟商户统计数据
	// 		$engineer_list = $Member->get_member_list(0,null,5,$this->member_id);
	// 	}else{
	// 		$engineer_list[0]['member_id'] = FALSE;
	// 	}
	// 	$data = get_order_count($engineer_list);

	// 	returnMobileJson(200,'获取成功',$data);
	// }

	/**
	 * 获取回收订单列表
	 * @param  int $offset 从第几天数据开始
	 * @param  int $limit  数据条数
	 * @param  int $status 订单状态，-1-未完成，0-待处理，1-待接单，2-已接单，3-待确认， 4-客户取消订单，5-待回收，6-已回收，7-已完成
	 * @param  int $member_id 成员id
	 * @return json 回收订单列表数据
	 */
	// public function recycle_order_list($offset = 0,$limit = 10,$status = FALSE){
	// 	/**
	// 	 * 获取回收订单数据
	// 	 * @param  array $engineer_list 工程师列表
	// 	 * @return [type]                [description]
	// 	 */


	// 	function get_order_list($engineer_list,$status){
	// 		$RecycleOrder = new RecycleOrderModel();
	// 		foreach ($engineer_list as $key => $engineer) {
	// 			$recycle_order_list = $RecycleOrder->get_order_list($status,$engineer['member_id']);
	// 		}
	// 		return $recycle_order_list;
	// 	}
	// 	$Member = new MemberModel();
	// 	if(!$this->menber_id){
	// 		$m_type = $Member->get_member($this->member_id)['m_type'];
	// 	}else{
	// 		$m_type = 0;
	// 	}
	// 	if($m_type==1){
	// 		// 城市合伙人统计数据
	// 		$have_engineer_list = $Member->get_member_list(0,null,3,$this->member_id);
	// 		$didi_engineer_list = $Member->get_member_list(0,null,4,$this->member_id);
	// 		$union_business_list = $Member->get_member_list(0,null,2,$this->member_id);
	// 		$union_engineer_list = array();
	// 		foreach ($union_business_list as $key => $union_business) {
	// 			$union_engineer_arr = $Member->get_member_list(0,null,5,$union_business['member_id']);
	// 			$union_engineer_list = array_merge($union_engineer_list,$union_engineer_arr);
	// 		}
	// 		$engineer_list = array_merge($have_engineer_list,$didi_engineer_list,$union_engineer_list);
	// 	}else if($m_type==2){
	// 		// 联盟商户统计数据
	// 		$engineer_list = $Member->get_member_list(0,null,5,$this->member_id);
	// 	}else{
	// 		$engineer_list[0]['member_id'] = FALSE;
	// 	}
	// 	$recycle_order_list = array_slice(get_order_list($engineer_list,$status),$offset,$limit);
	// 	$Option = new OptionModel();
	// 	foreach ($recycle_order_list as $key => $recycle_order) {
	// 		$mp_info_arr = json_decode($recycle_order['mp_info'],true);
	// 		foreach ($mp_info_arr as $mp_info_key => $mp_info) {
	// 			$mp_info_arr[$mp_info_key] = $Option->get_option($mp_info)['o_label'];
	// 		}
	// 		$recycle_order_list[$key]['update_time'] = date('Y-m-d H:i',strtotime($recycle_order['update_time']));
	// 		switch ($recycle_order['m_type']) {
	// 			case '1':
	// 				$recycle_order_list[$key]['member'] = '城市合伙人';
	// 				break;
	// 			case '3':
	// 				$recycle_order_list[$key]['member'] = '自有工程师';
	// 				break;
	// 			case '4':
	// 				$recycle_order_list[$key]['member'] = '滴滴工程师';
	// 				break;
	// 			case '5':
	// 			case '6':
	// 				$recycle_order_list[$key]['member'] = '联盟商户';
	// 				break;
	// 			default:
	// 				$recycle_order_list[$key]['member'] = '城市合伙人';
	// 				break;
	// 		}
	// 		$recycle_order_list[$key]['config_item'] = rtrim(implode('、',$mp_info_arr), '、');
	// 		unset($recycle_order_list[$key]['mp_info']);
	// 		unset($recycle_order_list[$key]['m_type']);
	// 	}
	// 	if(empty($recycle_order_list)){
	// 		returnMobileJson(201,'没有数据');
	// 	}else{
	// 		returnMobileJson(200,'获取成功',$recycle_order_list);
	// 	}
	// }

	public function recycle_order_list($offset = 0,$limit = 10,$status = FALSE,$repair_way=FALSE){
		$Member = new MemberModel();

		if($this->member_id){
			$member = $Member->get_member($this->member_id);
			if($member['m_type']==1){
				$Region = new RegionModel();
				$region_list = $Region->get_region($member['city_id']);
				$recycle_order_list = array();
				foreach ($region_list as $key => $region) {
					$recycle_order_arr = $this->RecycleOrder->get_order_list($status,$region['re_id'],$repair_way);
					$recycle_order_list = array_merge($recycle_order_list,$recycle_order_arr);
				}
			}else if($member['m_type']==2){
				$union_engineer_list = $Member->get_member_list($this->member_id);
				foreach ($union_engineer_list as $key => $union_engineer) {
					$recycle_order_arr = $this->RecycleOrder->get_order_list($status,$union_engineer['re_id'],$repair_way);
					$recycle_order_list = array_merge($recycle_order_list,$recycle_order_arr);
				}
			}
		}else{
			$recycle_order_list = $this->RecycleOrder->get_order_list($status,'',$repair_way);
		}
		$recycle_order_list = array_slice($recycle_order_list, $offset, $limit);
		$Option = new OptionModel();
		foreach ($recycle_order_list as $key => $recycle_order) {
			$mp_info_arr = json_decode($recycle_order['mp_info'],true);
			foreach ($mp_info_arr as $mp_info_key => $mp_info) {
				$mp_info_arr[$mp_info_key] = $Option->get_option($mp_info)['o_label'];
			}
			$recycle_order_list[$key]['update_time'] = date('Y-m-d H:i',strtotime($recycle_order['update_time']));
			switch ($recycle_order['m_type']) {
				case '1':
					$recycle_order_list[$key]['member'] = '城市合伙人';
					break;
				case '3':
					$recycle_order_list[$key]['member'] = '自有工程师';
					break;
				case '4':
					$recycle_order_list[$key]['member'] = '滴滴工程师';
					break;
				case '5':
				case '6':
					$recycle_order_list[$key]['member'] = '联盟商户';
					break;
				default:
					$recycle_order_list[$key]['member'] = null;
					break;
			}
			$recycle_order_list[$key]['config_item'] = rtrim(implode('、',$mp_info_arr), '、');
			unset($recycle_order_list[$key]['mp_info']);
			unset($recycle_order_list[$key]['m_type']);
		}
		if(empty($recycle_order_list)){
			returnMobileJson(201,'没有数据');
		}else{
			returnMobileJson(200,'获取成功',$recycle_order_list);
		}
	}


	/**
	 * 导出表单
	 */
	public function export_orderList_type1_excel($repair_way=FALSE){
		$Option = new OptionModel();
		$recycle_order_list = $this->RecycleOrder->get_order_list('','',$repair_way);
		foreach ($recycle_order_list as $key => $recycle_order) {
			$mp_info_arr = json_decode($recycle_order['mp_info'],true);
			foreach ($mp_info_arr as $mp_info_key => $mp_info) {
				$mp_info_arr[$mp_info_key] = $Option->get_option($mp_info)['o_label'];
			}
			$recycle_order_list[$key]['update_time'] = date('Y-m-d H:i',strtotime($recycle_order['update_time']));
			switch ($recycle_order['m_type']) {
				case '1':
					$recycle_order_list[$key]['member'] = '城市合伙人';
					break;
				case '3':
					$recycle_order_list[$key]['member'] = '自有工程师';
					break;
				case '4':
					$recycle_order_list[$key]['member'] = '滴滴工程师';
					break;
				case '5':
				case '6':
					$recycle_order_list[$key]['member'] = '联盟商户';
					break;
				default:
					$recycle_order_list[$key]['member'] = null;
					break;
			}
			$recycle_order_list[$key]['config_item'] = rtrim(implode('、',$mp_info_arr), '、');
			unset($recycle_order_list[$key]['mp_info']);
			unset($recycle_order_list[$key]['m_type']);
		}
		if($repair_way==1){
			//下单时间、订单编号、订单状态、订单金额、订单留言、客户姓名、手机号码、地区、详细地址、上门时间、机型、维修信息（回收信息）、师傅姓名、师傅手机号码
			$expCellName = array(
					array('update_time','下单时间'),
					array('order_num','订单编号'),
					array('status','订单状态'),
					array('price','订单价格'),
					array('phone_number','手机号码'),
					array('address','详细地址'),
					array('door_time','上门时间'),
					array('v_name','机型'),
					array('m_name','师傅姓名'),
					array('m_account','师傅手机号码'),
					array('config_item','回收信息')
			);
		}else{
			//下单时间、订单编号、订单状态、订单金额、订单留言、客户姓名、手机号码、地区、机型、维修信息（回收信息）、门店名称、门店电话
			$expCellName = array(
					array('update_time','下单时间'),
					array('order_num','订单编号'),
					array('status','订单状态'),
					array('price','订单价格'),
					array('phone_number','手机号码'),
					array('address','详细地址'),
					array('door_time','上门时间'),
					array('v_name','机型'),
					array('m_name','门店名称'),
					array('m_account','门店电话'),
					array('config_item','回收信息')
			);
		}
		$post['expTitle'] = '1';
		$post['expTableData'] = json_encode($recycle_order_list);
		$post['expCellName'] = json_encode($expCellName);
		header('pragma:public');
		header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $post['expTitle'] . '.xls"');
		header("Content-Disposition:attachment;filename=1.xls");
		echo Requests::post('http://www.gojiw.com/gojiw/index/index_recycle/index.php?m=Home&c=Excel&a=getData',$post);

	}


	/**
	 * 获取城市订单
	 */
	public function city_order_list(){
		$status = $_REQUEST['status'];
		$RecycleOrder = new RecycleOrderModel();
		$Member = new MemberModel();
		$region_member = $Member->get_member($this->member_id);

		$recycle_order_list = $RecycleOrder->get_order_list($status,$region_member['city_id']);
		returnMobileJson(200,'获取成功',$recycle_order_list);
	}

	/**
	 * 指派回收订单
	 */
	public function assign_recycle_order(){
		$rid = $_POST['rid'];
		$data['mid'] = $_POST['member_id'];
		$data['status'] = 1;
		if(empty($data['mid'])){
			returnMobileJson(201,'参数错误');
		}
		if($this->RecycleOrder->set_order($data,$rid)){
			$Member = new MemberModel();
			$memberData = $Member->get_member($data['mid']);
			if($memberData['openid']){
				$RecycleOrder = new RecycleOrderModel();
				$order_data = $RecycleOrder->get_order($rid);
				$dataJson['openid']=$memberData['openid'];
				$dataJson['order_info'] = json_encode($order_data);
				Requests::post('http://www.gojiw.com/gojiw/index/index_recycle/index.php?m=Home&c=Wx&a=sendMasterOrder',$dataJson);
				returnMobileJson(200,'指派成功并推送了订单');
			}

			returnMobileJson(200,'指派成功');
		}else{
			returnMobileJson(202,'指派失败');
		}
	}

	/**
	 * 获取地区工程师
	 * @param  int $re_id 地区id
	 */
	public function get_region_engineer($re_id){
		$Member = new MemberModel();
		$region_member = $Member->get_region_member($re_id);
		if(empty($region_member)){
			returnMobileJson(200,'success',array());
		}else{
			returnMobileJson(200,'获取成功',$region_member);
		}
	}

	/**
	 * 获取某个工程师的订单
	 */
	public function get_member_order_count(){
		$member_id = $_REQUEST['member_id'];
		$RecycleOrder = new RecycleOrderModel();
		$data['total'] = $RecycleOrder->get_order_count2($member_id);
//		$data['waiting_handle'] = $RecycleOrder->get_order_count(FALSE,FALSE,0,$member_id);
//		$data['waiting_list'] = $RecycleOrder->get_order_count(FALSE,FALSE,1,$member_id);
//		$data['waiting_recycle'] = $RecycleOrder->get_order_count(FALSE,FALSE,5,$member_id);
//		$data['waiting_confirm'] = $RecycleOrder->get_order_count(FALSE,FALSE,3,$member_id);
//		$data['completed'] = $RecycleOrder->get_order_count(FALSE,FALSE,7,$member_id);
		returnMobileJson(200,'获取成功',$data);
	}


	/**
	 * 提现申请
	 * @param bool|false $m_type
	 * @param bool|false $a_audit_status
	 * @param int |1 $curPage
	 */
	public function apply_withdraw_list($m_type=false,$a_audit_status = false,$curPage=1,$is=false){

		if($_SESSION['member_id']!=0){
			returnMobileJson(-2,'无权限操作');
		}

		$Member = new MemberModel();
		$account_details_db = M('account_details');
		$condition = array();
		$m_type && $condition['m_type'] = $m_type;
		$a_audit_status && $condition['a_audit_status'] = $a_audit_status;
		$condition['a_status'] = 2;
		$data = $account_details_db->where($condition)->page($curPage,10)->order('aid desc')->select();
		foreach($data as $key=>$value){
			$data[$key]['a_date_format'] = date('Y-m-d H:i:s',$value['a_date']);
			$data[$key]['member'] = $Member->get_member($value['member_id']);
		}
		if($is){
			return $data;
		}else{
			returnMobileJson(200,'获取成功',$data);
		}

	}

	/**
	 * 提现申请
	 * @param bool|false $m_type
	 * @param bool|false $a_audit_status
	 * @param int |1 $curPage
	 */
	public function apply_withdraw_jy_list($a_audit_status = false,$curPage=1){

		if($_SESSION['member_id']!=0){
			returnMobileJson(-2,'无权限操作');
		}



		$Member = M('user');
		$account_details_db = M('jy_account_details');
		$condition = array();
		if($a_audit_status==1 || $a_audit_status==0){
			$condition['a_audit_status'] = $a_audit_status;
		}
		$condition['a_status'] = 2;
		$data = $account_details_db->where($condition)->page($curPage,10)->order('aid desc')->select();
		foreach($data as $key=>$value){
			$data[$key]['a_date_format'] = date('Y-m-d H:i:s',$value['a_date']);
			$data[$key]['user'] = $Member->where(array('uid'=>$value['uid']))->find();
		}

		returnMobileJson(200,'获取成功',$data);
	}


	/**
	 * 同意提现申请
	 */
	public function agreement_withdraw(){
		if($_SESSION['member_id']!=0){
			returnMobileJson(-2,'无权限操作');
		}
		$account_details_db = M('account_details');
		$aid = $_POST['aid'];
		$map = array();
		$map['aid'] = $aid;

		if(is_numeric($aid)){
			$Member = new MemberModel();
			$result_data = $account_details_db->where($map)->find();
			if(!$result_data['aid']){
				returnMobileJson(-2,'参数异常');
			}
			if($result_data['a_audit_status']==1){
				returnMobileJson(-2,'此笔订单已提现');
			}
			$member = $Member->get_member($result_data['member_id']);
			$postData = array();
			$postData['amount'] = $result_data['a_money']*100;
			$postData['partner_trade_no'] = 'a'.$result_data['aid'];
			$postData['openid'] = $member['openid'];
			$json = Requests::post('http://www.gojiw.com/gojiw/index/index/admin.php?c=WithDraw&a=do_pay_api',$postData);
			$arr = json_decode($json);
			if($arr['result_code']=='FAIL'){
				returnMobileJson(-2,$arr['err_code_des']);
			}elseif($arr['result_code']=='SUCCESS'){
				$account_details_db->where($map)->save(array('a_audit_status'=>1));
				returnMobileJson(200,'操作成功');
			}else{
				$account_details_db->where($map)->save(array('a_audit_status'=>1));
				returnMobileJson(200,'操作成功');
			}
		}else{
			returnMobileJson(200,'参数有误');
		}


	}

	/**
	 * 同意用户提现申请
	 */
	public function agreement_jy_withdraw(){
		if($_SESSION['member_id']!=0){
			returnMobileJson(-2,'无权限操作');
		}
		$account_details_db = M('jy_account_details');
		$aid = $_POST['aid'];
		$map = array();
		$map['aid'] = $aid;

		if(is_numeric($aid)){
			$Member = M('user');
			$result_data = $account_details_db->where($map)->find();
			if(!$result_data['aid']){
				returnMobileJson(-2,'参数异常');
			}
			if($result_data['a_audit_status']==1){
				returnMobileJson(-2,'此笔订单已提现');
			}
			$member = $Member->where(array('uid'=>$result_data['uid']))->find();
			$postData = array();
			$postData['amount'] = $result_data['a_money']*100;
			$postData['partner_trade_no'] = 'b'.$result_data['aid'];
			$postData['openid'] = $member['openid'];
			$json = Requests::post('http://www.gojiw.com/gojiw/index/index/admin.php?c=WithDraw&a=do_pay_api',$postData);
			$arr = json_decode($json);
			if($arr['result_code']=='FAIL'){
				returnMobileJson(-2,$arr['err_code_des']);
			}elseif($arr['result_code']=='SUCCESS'){
				$account_details_db->where($map)->save(array('a_audit_status'=>1));
				returnMobileJson(200,'操作成功');
			}else{
				$account_details_db->where($map)->save(array('a_audit_status'=>1));
				returnMobileJson(200,'操作成功');
			}
		}else{
			returnMobileJson(200,'参数有误');
		}


	}


	/**
	 * 导出excell
	 * @param $expTitle
	 * @param $expCellName
	 * @param $expTableData
	 */
	private function exportExcel($expTitle,$expCellName,$expTableData){
		$xlsTitle = iconv('utf-8', 'gb2312', $expTitle); //文件名称
		$fileName = $expTitle . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
		$cellNum = count($expCellName);
		$dataNum = count($expTableData);
		vendor("PHPExcel.PHPExcel");
		$objPHPExcel = new \PHPExcel();
		$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
//  $objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1'); //合并单元格
		for ($i = 0; $i < $cellNum; $i++) {
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '1', $expCellName[$i][1]);
		}
		// Miscellaneous glyphs, UTF-8
		for ($i = 0; $i < $dataNum; $i++) {
			for ($j = 0; $j < $cellNum; $j++) {
				$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 2), $expTableData[$i][$expCellName[$j][0]]);
			}
		}

		$objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

		header('pragma:public');
		header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
		header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
		exit;
	}

	/**
	 * 导出数据为excel表格
	 *@param $data    一个二维数组,结构如同从数据库查出来的数组
	 *@param $title   excel的第一行标题,一个数组,如果为空则没有标题
	 *@param $filename 下载的文件名
	 *@examlpe
	$stu = M ('User');
	$arr = $stu -> select();
	exportexcel($arr,array('id','账户','密码','昵称'),'文件名!');
	 */
	private function exportexcel_new($data=array(),$title=array(),$filename='report'){
		header("Content-type:application/octet-stream");
		header("Accept-Ranges:bytes");
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:attachment;filename=".$filename.".xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		//导出xls 开始
		if (!empty($title)){
			foreach ($title as $k => $v) {
				$title[$k]=iconv("UTF-8", "GB2312",$v);
			}
			$title= implode("\t", $title);
			echo "$title\n";
		}
		if (!empty($data)){
			foreach($data as $key=>$val){
				foreach ($val as $ck => $cv) {
					$data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);
				}
				$data[$key]=implode("\t", $data[$key]);

			}
			echo implode("\n",$data);
		}
	}



	/**
	 * 导出提现表
	 * @param bool|false $m_type
	 * @param bool|false $a_audit_status
	 * @param int $curPage
	 */
	public function apply_withdraw_list_excell($m_type=false,$a_audit_status = false,$curPage=1){
		$data = $this->apply_withdraw_list($m_type,$a_audit_status,$curPage,true);
		$newData = array();
		foreach($data as $key=>$value){
			$newData[$key]['m_account'] =  $value['member']['m_account'];
			$newData[$key]['a_money'] = $value['a_money'];
			$newData[$key]['m_money'] = $value['member']['m_money'];
			$newData[$key]['a_date_format'] = $value['a_date_format'];
		}
//		$expCellName = array('账号','提现金额','剩余金额','提现日期');
		$expCellName = array(array('m_account','账号'),array('a_money','提现金额'),array('m_money','剩余金额'),array('a_date_format','提现日期'));
//		$this->exportexcel_new($newData,$expCellName,'new');
		$this->exportExcel('1',$expCellName,$newData);
	}


}
